package com.icow.wms.basic.controller;

import java.util.List;

import com.icow.wms.basic.domain.Lift;
import com.icow.wms.basic.utils.LiftHandler;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.icow.common.annotation.Log;
import com.icow.common.core.controller.BaseController;
import com.icow.common.core.domain.AjaxResult;
import com.icow.common.enums.BusinessType;
import com.icow.wms.basic.domain.LiftPeripheral;
import com.icow.wms.basic.service.ILiftPeripheralService;
import com.icow.common.utils.poi.ExcelUtil;
import com.icow.common.core.page.TableDataInfo;

/**
 * liftController
 *
 * @author bcs
 * @date 2024-09-02
 */
@RestController
@RequestMapping(value = {"/peripheral/lift", "/api"})
public class LiftPeripheralController extends BaseController {
    @Autowired
    private ILiftPeripheralService liftPeripheralService;


    @GetMapping(value = "/lift/{id}")
    public AjaxResult callLift(@PathVariable Long id) {
        LiftPeripheral liftPeripheral = liftPeripheralService.selectLiftPeripheralById(id);
        Lift lift = LiftHandler.IP_LIFT_MAP.get(liftPeripheral.getIp());
        if (lift == null) {
            return AjaxResult.error("电梯" + liftPeripheral.getIp() + "不在线");
        }
        boolean callLift = lift.callLift(liftPeripheral.getIp(), liftPeripheral.getCallLiftCommand());
        if (!callLift) {
            return AjaxResult.error("电梯" + liftPeripheral.getIp() + "呼梯失败");
        }
        lift.init(liftPeripheral.getCallLiftCommand(), liftPeripheral.getLiftReachCommand(), liftPeripheral.getStopCallLiftCommand(), liftPeripheral.getFloor());
        lift.canClearLiftState();
        return AjaxResult.success("呼梯成功");
    }

    @GetMapping(value = "/readLift/{id}")
    public AjaxResult readLift(@PathVariable Long id) {
        LiftPeripheral liftPeripheral = liftPeripheralService.selectLiftPeripheralById(id);
        Lift lift = LiftHandler.IP_LIFT_MAP.get(liftPeripheral.getIp());
        if (lift == null) {
            return AjaxResult.error("电梯" + liftPeripheral.getIp() + "不在线");
        }
        boolean reachLift = lift.readLiftState(liftPeripheral.getIp(), liftPeripheral.getReadLiftReachCommand(), Integer.parseInt(liftPeripheral.getFloor()));
        if (!reachLift) {
            return AjaxResult.error("电梯" + liftPeripheral.getIp() + "不在到位指令对应位置");
        }
        return AjaxResult.success("电梯到位了");
    }

    /**
     * 查询lift列表
     */
    @PreAuthorize("@ss.hasPermi('peripheral:lift:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiftPeripheral liftPeripheral) {
        startPage();
        List<LiftPeripheral> list = liftPeripheralService.selectLiftPeripheralList(liftPeripheral);
        return getDataTable(list);
    }

    /**
     * 导出lift列表
     */
    @PreAuthorize("@ss.hasPermi('peripheral:lift:export')")
    @Log(title = "lift", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LiftPeripheral liftPeripheral) {
        List<LiftPeripheral> list = liftPeripheralService.selectLiftPeripheralList(liftPeripheral);
        ExcelUtil<LiftPeripheral> util = new ExcelUtil<LiftPeripheral>(LiftPeripheral.class);
        return util.exportExcel(list, "lift");
    }

    /**
     * 获取lift详细信息
     */
    @PreAuthorize("@ss.hasPermi('peripheral:lift:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(liftPeripheralService.selectLiftPeripheralById(id));
    }

    /**
     * 新增lift
     */
    @PreAuthorize("@ss.hasPermi('peripheral:lift:add')")
    @Log(title = "lift", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiftPeripheral liftPeripheral) {
        return toAjax(liftPeripheralService.insertLiftPeripheral(liftPeripheral));
    }

    /**
     * 修改lift
     */
    @PreAuthorize("@ss.hasPermi('peripheral:lift:edit')")
    @Log(title = "lift", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiftPeripheral liftPeripheral) {
        return toAjax(liftPeripheralService.updateLiftPeripheral(liftPeripheral));
    }

    /**
     * 删除lift
     */
    @PreAuthorize("@ss.hasPermi('peripheral:lift:remove')")
    @Log(title = "lift", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(liftPeripheralService.deleteLiftPeripheralByIds(ids));
    }
}